#!/bin/bash

if [ "$1" == "-help" ];then
printf "
manual:
Hello user! Thank you for using the program!
This program only print ships according its coordinates and rules of battleship
For printing ships enter its coordinates of begining and finishing in format: 
LetterNumberLetterNumber(a1a1) or NumberNumberNumberNumber(0101) or LetterLetterLetterLetter(abab)
The program follows next rules:
0) Coordinates must be from 0 to 9 or from a to j
1) Length of the ship from 1 to 4
2) The program don't check number of ships! ( in order to reduce time of testing the program by the teacher ;) )
3) Ships must not be closer than 1 cell
4) Ships must not have bends
5) If the coordinates do not satisfy the requirements 0-4, than program won't print the field and will print you about error
6) One cell is two signs ("__" -empty cell/"HH" -cell with a part of the ship)\n"
exit 0
fi
#Functions:
function translator(){
	case $1 in
	[0-9])
		return $1;;
	"a")
		return 0;;
	"b")
		return 1;;
	"c")
		return 2;;
	"d")
		return 3;;
	"e")
		return 4;;
	"f")
		return 5;;
	"g")
		return 6;;
	"h")
		return 7;;
	"i")
		return 8;;
	"j")
		return 9;;
	*)
		echo "Error in parametrs with $1" >&2
		echo "Remember, support only low register a..j and figures 0..9!" >&2
	exit -2;;
	esac
}
#main:
echo
echo "   Battleship field"
str=$*
str+=" " #else algorithm don't work correctly :(, in my opinion it's easier, than create another
len=${#str}
flag=false
counter=0

for ((i=0; i<len; i++))
do
	if [ "${str:i:1}" != " " ]; then
		translator "${str:i:1}"
		coor[counter]=$?
		((counter++))
	else
		if [ $counter != 4 ]; then
			echo "Error in parametrs!" >&2
			exit -1
		else
			for ((index=0; index<4; index+=2));do
				for ((e=-1; e<1; e++));do
					let "tmp=(coor[index+1]+e)*10 + coor[index]"
					for ((r=-1; r<1; r++));do
						let "temp = tmp+r"
						let "check1 = temp/10"
						let "check2 = tmp/10"	
						if [ $temp -ge 0 ] && [ $check1 -eq $check2 ] && [ "${map[$temp]}" == "H" ] ; then
							echo "Error in parametrs! Ships intersect or too close! Point:" "${coor[0]}${coor[1]}${coor[2]}${coor[3]}" >&2	
							exit -5;
						fi
					done
				done
			done
			if [ "${coor[0]}" == "${coor[2]}" ]; then
				if [ ${coor:1:1} -le ${coor:3:1} ]; then
					sign="1";
				else
					sign="-1";				
				fi				
				let "l = sign * coor[3] - sign * coor[1]"
				if [ $l -gt 3 ];then
					echo "Error in parametrs! Too long ship! $l" "${coor[0]}${coor[1]}${coor[2]}${coor[3]}" >&2		
					exit -6
				fi
				for((j=0; j<=l; j++))
				do
					let "temp = (coor[1]+j)*10 + coor[0]"
					map[temp]="H"
				done 
			elif [ "${coor[1]}" == "${coor[3]}" ]; then
				if [ ${coor[0]} -le ${coor[2]} ]; then
					sign="1";
				else
					sign="-1";				
				fi				
				let "l = sign * coor[2] - sign * coor[0]"
				if [ $l -gt 3 ];then
					echo "Error in parametrs! Too long ship! $l" "${coor[0]}${coor[1]}${coor[2]}${coor[3]}" >&2		
					exit -6
				fi
				for((j=0; j<=l; j++))
				do
					let "temp = coor[1]*10 + coor[0]+j"
					map[temp]="H"
				done		
			else
				echo "Error in parametrs!" "${coor[0]}" "${coor[1]}" "${coor[2]}" "${coor[3]}" >&2
				exit -4
			fi			
			counter=0
		fi		
	fi
done
#Print:
echo "  0 1 2 3 4 5 6 7 8 9"
echo -n "0 "
for((j=1; j<101; j++))
do	
	if [ "${map[j-1]}" == "" ];then
		echo -n ". "
	else
		echo -n "${map[j-1]} "
	fi
	let "q = j % 10"
	let "w = j / 10"
	if [ $q == "0" ];then
		echo
	if [ $j != "100" ];then		
		echo -n "$w "
	fi
	fi 
done
printf "\nPlease, read the manual (bash battleship.sh -help) for better using\n"
